package com.zhang.sys;

import com.ygdk.util.ListUtil;
import com.ygdk.util.StringUtil;
import com.zhang.BaseHibernateDAO;
import com.zhang.bean.GisSysConditionBean;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * Upload entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.zhang.sys.Upload
 * @author MyEclipse Persistence Tools
 */

public class UploadDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(UploadDAO.class);
	// property constants
	public static final String WJM = "wjm";
	public static final String WJDX = "wjdx";
	public static final String WJLX = "wjlx";
	public static final String SCWJM = "scwjm";
	public static final String SFYJDR = "sfyjdr";
	public static final String RKRBH = "rkrbh";
	public static final String RKRXM = "rkrxm";
	public static final String RKDWBH = "rkdwbh";
	public static final String RKDWXM = "rkdwxm";
	public static final String RKSJ = "rksj";

	public void save(Upload transientInstance) {
		log.debug("saving Upload instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Upload persistentInstance) {
		log.debug("deleting Upload instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Upload findById(java.lang.String id) {
		log.debug("getting Upload instance with id: " + id);
		try {
			Upload instance = (Upload) getSession().get("com.zhang.sys.Upload",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Upload instance) {
		log.debug("finding Upload instance by example");
		try {
			List results = getSession().createCriteria("com.zhang.sys.Upload")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Upload instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Upload as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByWjm(Object wjm) {
		return findByProperty(WJM, wjm);
	}

	public List findByWjdx(Object wjdx) {
		return findByProperty(WJDX, wjdx);
	}

	public List findByWjlx(Object wjlx) {
		return findByProperty(WJLX, wjlx);
	}

	public List findByScwjm(Object scwjm) {
		return findByProperty(SCWJM, scwjm);
	}

	public List findBySfyjdr(Object sfyjdr) {
		return findByProperty(SFYJDR, sfyjdr);
	}

	public List findByRkrbh(Object rkrbh) {
		return findByProperty(RKRBH, rkrbh);
	}

	public List findByRkrxm(Object rkrxm) {
		return findByProperty(RKRXM, rkrxm);
	}

	public List findByRkdwbh(Object rkdwbh) {
		return findByProperty(RKDWBH, rkdwbh);
	}

	public List findByRkdwxm(Object rkdwxm) {
		return findByProperty(RKDWXM, rkdwxm);
	}

	public List findByRksj(Object rksj) {
		return findByProperty(RKSJ, rksj);
	}

	public List findAll() {
		log.debug("finding all Upload instances");
		try {
			String queryString = "from Upload";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Upload merge(Upload detachedInstance) {
		log.debug("merging Upload instance");
		try {
			Upload result = (Upload) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Upload instance) {
		log.debug("attaching dirty Upload instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Upload instance) {
		log.debug("attaching clean Upload instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public List findByCondition(List list) {
		log.debug("finding all Upload instances");
		ListUtil.sortConditionList(list);
		List listSum = ListUtil.parseConditionList(list);
		String str = StringUtil.parseCondition(listSum, "kqxx");
		List listTemp;
		GisSysConditionBean gcb;
		String param;
		int index;
		try {
			StringBuffer queryString = new StringBuffer();
			queryString.append("from Upload kqxx");
			if (str.length() > 0) {
				queryString.append(" where ");
				queryString.append(str);
			}
			queryString.append(" order by kqxx.rksj desc");
			Query queryObject = getSession()
					.createQuery(queryString.toString());
			for (int i = 0; i < listSum.size(); i++) {
				listTemp = (ArrayList) listSum.get(i);
				for (int j = 0; j < listTemp.size(); j++) {
					gcb = (GisSysConditionBean) listTemp.get(j);
					if(!"is".equals(gcb.getOper())){
						index = gcb.getName().lastIndexOf(".");
						if (index > 0) {
							param = gcb.getName().substring(index + 1);
						} else {
							param = gcb.getName();
						}
						if ("like".equals(gcb.getOper())) {
							queryObject.setParameter(param + j,
									"%" + gcb.getValue() + "%");
						} else if ("~".equals(gcb.getOper())) {
							String[] valueArr = gcb.getValue().split("~");
							queryObject.setParameter(param + j + "0", valueArr[0]);
							queryObject.setParameter(param + j + "1", valueArr[1]);
						}else if("is".equals(gcb.getOper())){
							
						}
						else {
							queryObject.setParameter(param + j, gcb.getValue());
						}
					}
				}
			}
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByCondition(List list, int start, int limit) {
		log.debug("finding all Upload instances");
		ListUtil.sortConditionList(list);
		List listSum = ListUtil.parseConditionList(list);
		String str = StringUtil.parseCondition(listSum, "kqxx");
		List listTemp;
		GisSysConditionBean gcb;
		String param;
		int index;
		try {
			StringBuffer queryString = new StringBuffer();
			queryString.append("from Upload kqxx");
			if (str.length() > 0) {
				queryString.append(" where ");
				queryString.append(str);
			}
			queryString.append(" order by kqxx.rksj desc");
			Query queryObject = getSession()
					.createQuery(queryString.toString());
			for (int i = 0; i < listSum.size(); i++) {
				listTemp = (ArrayList) listSum.get(i);
				for (int j = 0; j < listTemp.size(); j++) {
					gcb = (GisSysConditionBean) listTemp.get(j);
					if(!"is".equals(gcb.getOper())){
						index = gcb.getName().lastIndexOf(".");
						if (index > 0) {
							param = gcb.getName().substring(index + 1);
						} else {
							param = gcb.getName();
						}
						if ("like".equals(gcb.getOper())) {
							queryObject.setParameter(param + j,
									"%" + gcb.getValue() + "%");
						} else if ("~".equals(gcb.getOper())) {
							String[] valueArr = gcb.getValue().split("~");
							queryObject.setParameter(param + j + "0", valueArr[0]);
							queryObject.setParameter(param + j + "1", valueArr[1]);
						}else if("is".equals(gcb.getOper())){
							
						}
						else {
							queryObject.setParameter(param + j, gcb.getValue());
						}
					}
				}
			}
			queryObject.setFirstResult(start);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public int countByCondition(List list) {
		log.debug("finding Upload instances");
		ListUtil.sortConditionList(list);
		List listSum = ListUtil.parseConditionList(list);
		String str = StringUtil.parseCondition(listSum, "kqxx");
		List listTemp;
		GisSysConditionBean gcb;
		String param;
		int index;
		try {

			StringBuffer queryString = new StringBuffer();
			queryString.append("select count(kqxx) from Upload kqxx");
			if (str.length() > 0) {
				queryString.append(" where ");
				queryString.append(str);
			}
			Query queryObject = getSession()
					.createQuery(queryString.toString());
			for (int i = 0; i < listSum.size(); i++) {
				listTemp = (ArrayList) listSum.get(i);
				for (int j = 0; j < listTemp.size(); j++) {
					gcb = (GisSysConditionBean) listTemp.get(j);
					if(!"is".equals(gcb.getOper())){
						index = gcb.getName().lastIndexOf(".");
						if (index > 0) {
							param = gcb.getName().substring(index + 1);
						} else {
							param = gcb.getName();
						}
						if ("like".equals(gcb.getOper())) {
							queryObject.setParameter(param + j,
									"%" + gcb.getValue() + "%");
						} else if ("~".equals(gcb.getOper())) {
							String[] valueArr = gcb.getValue().split("~");
							queryObject.setParameter(param + j + "0", valueArr[0]);
							queryObject.setParameter(param + j + "1", valueArr[1]);
						}else if("is".equals(gcb.getOper())){
							
						}
						else {
							queryObject.setParameter(param + j, gcb.getValue());
						}
					}
				}
			}
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}